package flc.ast.activity;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import flc.ast.fragment.TimeCalFragment;
import flc.ast.fragment.TimeConvertFragment;
import flc.ast.fragment.TimeIntervalFragment;

import android.graphics.Color;
import android.graphics.Typeface;
import android.view.View;

import java.text.ParseException;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityTimeBinding;
import flc.ast.utils.DateAndTimeDialog;

public class TimeActivity extends BaseAc<ActivityTimeBinding> implements DateAndTimeDialog.TimePickerDialogInterface {

    private Fragment mShowFragment;
    private int flag=0;
    private TimeIntervalFragment timeIntervalFragment = new TimeIntervalFragment();
    private TimeConvertFragment timeConvertFragment=new TimeConvertFragment();
    private TimeCalFragment timeCalFragment=new TimeCalFragment();



    @Override
    protected int onCreate() {
        return R.layout.activity_time;
    }

    @Override
    protected void initView() {
        mDataBinding.ivTimeBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.tvTimeKind1.setOnClickListener(this);
        mDataBinding.tvTimeKind2.setOnClickListener(this);
        mDataBinding.tvTimeKind3.setOnClickListener(this);
        showTimeIntervalFragment();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvTimeKind1:
                clearSelection();
                showTimeIntervalFragment();
                mDataBinding.llTimeKindTitle.setBackgroundResource(R.drawable.ak4);
                mDataBinding.tvTimeKind1.setTextColor(Color.WHITE);
                mDataBinding.tvTimeKind1.setTypeface(Typeface.DEFAULT_BOLD);
                mDataBinding.tvTimeKind1.setBackgroundResource(R.drawable.ak5);
                break;
            case R.id.tvTimeKind2:
                clearSelection();
                showTimeConvertFragment();
                mDataBinding.llTimeKindTitle.setBackgroundResource(R.drawable.ak7);
                mDataBinding.tvTimeKind2.setTextColor(Color.WHITE);
                mDataBinding.tvTimeKind2.setTypeface(Typeface.DEFAULT_BOLD);
                mDataBinding.tvTimeKind2.setBackgroundResource(R.drawable.ak5);
                break;
            case R.id.tvTimeKind3:
                clearSelection();
                showTimeCalFragment();
                mDataBinding.llTimeKindTitle.setBackgroundResource(R.drawable.ak8);
                mDataBinding.tvTimeKind3.setTextColor(Color.WHITE);
                mDataBinding.tvTimeKind3.setTypeface(Typeface.DEFAULT_BOLD);
                mDataBinding.tvTimeKind3.setBackgroundResource(R.drawable.ak5);
                break;
        }
    }

    @Override
    protected void initData() {

    }

    private void showTimeIntervalFragment() {
        flag=0;
        showFragment(timeIntervalFragment);
    }

    private void showTimeConvertFragment() {
        flag=1;
        showFragment(timeConvertFragment);
    }

    private void showTimeCalFragment() {
        flag=2;
        showFragment(timeCalFragment);
    }

    private void showFragment(Fragment fragment) {
        if (fragment == null) return;
        if (fragment.isAdded() && fragment.isVisible()) return;
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (mShowFragment != null) {
            transaction.hide(mShowFragment);
        }
        if (!fragment.isAdded()) {
            transaction.add(R.id.fl_container, fragment);
        } else {
            transaction.show(fragment);
        }
        mShowFragment = fragment;
        transaction.commit();
    }

    private void clearSelection() {
        mDataBinding.tvTimeKind1.setTextColor(Color.parseColor("#553CEB"));
        mDataBinding.tvTimeKind1.setBackground(null);
        mDataBinding.tvTimeKind1.setTypeface(Typeface.DEFAULT);
        mDataBinding.tvTimeKind2.setTextColor(Color.parseColor("#553CEB"));
        mDataBinding.tvTimeKind2.setBackground(null);
        mDataBinding.tvTimeKind2.setTypeface(Typeface.DEFAULT);
        mDataBinding.tvTimeKind3.setTextColor(Color.parseColor("#553CEB"));
        mDataBinding.tvTimeKind3.setBackground(null);
        mDataBinding.tvTimeKind3.setTypeface(Typeface.DEFAULT);
    }

    @Override
    public void positiveListener(int viewCode) {
        if (flag==0){
            try {
                timeIntervalFragment.myPositiveListener(viewCode);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }else if (flag==1){
            try {
                timeConvertFragment.myPositiveListener(viewCode);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }else if (flag==2){
            try {
                timeCalFragment.myPositiveListener(viewCode);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    public void negativeListener() {

    }
}